package com.woniuxy.happylife.order.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: RabbitmqConfiguration
 * @ProjectName: redis-rabbitmq
 * @Description: TODO
 * @Author: chowinggum
 * @Date: 2022/1/12 15:25
 */

/**
 * 定义交换机，死信交换机，队列，死信队列，绑定交换机
 */
@Configuration
public class RabbitmqConfiguration {
    //定义交换机
    @Bean
    public TopicExchange orderexchange(){
        return new TopicExchange("orderexchange");
    }
    //定义死信交换机
    @Bean
    public TopicExchange dieexchange(){
        return new TopicExchange("dieexchange");
    }
 //定义对列
    @Bean
    public Queue orderqueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","dieexchange");
        arguments.put("x-dead-letter-routing-key","die");

        return new Queue("orderqueue", true, false, false,arguments);
    }

    //定义死信队列
    @Bean
    public Queue diequeue(){
        return new Queue("diequeue");
    }

    //将order队列绑定到orderexchange交换机
    @Bean
    public Binding bindingOrder(TopicExchange orderexchange ,Queue orderqueue){
        return BindingBuilder.bind(orderqueue)
                .to(orderexchange).with("#.order.#");
    }

    //将diequeue队列绑定到死信交换机
    @Bean
    public Binding bindingDie(TopicExchange dieexchange,Queue diequeue){
        return BindingBuilder.bind(diequeue).to(dieexchange).with("#.die.#");
    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
